package org.yuanzheng.window

import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
import org.apache.flink.streaming.api.windowing.time.Time
import org.yuanzheng.source.StationLog

/**
 * @author yuanzheng
 * @date 2020/6/21-16:57
 */
object TestReduceFunction {
  def main(args: Array[String]): Unit = {

    val streamEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    import org.apache.flink.streaming.api.scala._

    //读取数据源
    val stream: DataStream[StationLog] = streamEnv.socketTextStream("192.168.1.8", 8888)
      .map(line => {
        var split = line.split(",")
        new StationLog(split(0).trim, split(1).trim, split(2).trim, split(3).trim, split(4).trim.toLong, split(5).trim.toLong)
      })

    //开窗
    stream.map(log => (log.sid, 1)).keyBy(_._1).timeWindow(Time.seconds(5)).reduce((t1, t2) => (t1._1, t1._2 + t2._2)).print()
    streamEnv.execute()
  }
}
